package CapaDatos.listas;

import CapaDatos.Restaurant;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.Iterator;

public class ListaRestaurants
{
    private TreeSet <Restaurant> restaurants;
    private Comparator comparator;

    public ListaRestaurants()
    {
        comparator  = new CompararRestaurants();
        restaurants = new TreeSet(comparator);
    }

    public boolean agregar (Restaurant objeto) {
        return restaurants.add(objeto);
    }

    public boolean actualizar (Restaurant restaurant) {
        if (!eliminar(restaurant.getID()))
            return false;

        return agregar (restaurant);
    }

    public boolean eliminar (String ID) {
        Restaurant auxiliar = buscar(ID);

        if (auxiliar == null)
            return false;

        return restaurants.remove(auxiliar);
    }

    public Restaurant buscar (String ID) {
        Restaurant auxiliar;
        Iterator iterator = restaurants.iterator();

        while (iterator.hasNext())
        {
            auxiliar = (Restaurant) iterator.next();

            if (auxiliar.getID().equals(ID))
                return auxiliar;
        }

        return null;
    }

    public Restaurant[] array() {
        return restaurants.toArray(new Restaurant[0]);
    }
}
